package breeze.pixel.weather;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Typeface;

import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import breeze.pixel.weather.apps_util.utils.API;
import breeze.pixel.weather.apps_util.utils.AppToolUtil;
import breeze.pixel.weather.apps_util.utils.appsettings.AppSettings;
import breeze.pixel.weather.error_view.service.ErrorCatchService;
import brz.breeze.app_utils.BApplication;
import brz.breeze.app_utils.BHotFixUtils;
import brz.breeze.tool_utils.Blog;

public class App extends BApplication {
	
	private static final String TAG = "App";
	private static AppSettings sets;

	public static Context getContext() {
		return context;
	}

	@SuppressLint("StaticFieldLeak")
	private static Context context;
	static {
		System.loadLibrary("app");
	}

	@Override
	public void onCreate() {
		super.onCreate();
		context = getApplicationContext();
		sets = AppSettings.getInstance(this);
		Typeface typeface = getTextFont(sets.getTextFont());
		//替换全局的字体
		AppToolUtil.changeGlobalTypeface(typeface);
		//初始化证书
		initCert();
		//LOG util
		Blog.init(getContext());
		//初始化错误日志
		Thread.setDefaultUncaughtExceptionHandler(ErrorCatchService.getInstance(this));
		//热修复
		try {
			BHotFixUtils.init(this, API._API_HOT_FIX, null);
			BHotFixUtils.fixApp();
			Blog.i(TAG, "补丁已修补");
		} catch (Exception exception) {
			exception.printStackTrace();
			Blog.e(exception);
		}
	}

	public static AppSettings getAppSettings(){
		return sets;
	}

	private void initCert(){
		//证书
		try{
			TrustManager[] trustManagers = new TrustManager[]{new X509TrustManager() {
				@SuppressLint("TrustAllX509TrustManager")
				@Override
				public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {

				}

				@SuppressLint("TrustAllX509TrustManager")
				@Override
				public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {

				}

				@Override
				public X509Certificate[] getAcceptedIssuers() {
					return new X509Certificate[0];
				}
			}};
			SSLContext sslContext = SSLContext.getInstance("TLS");
			sslContext.init(null, trustManagers, new SecureRandom());
			HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
			HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
				@SuppressLint("BadHostnameVerifier")
				@Override
				public boolean verify(String hostname, SSLSession session) {
					return true;
				}
			});
		}catch (Exception e){
			e.printStackTrace();
		}
	}

	public static String getFileProvider(){
		return context.getPackageName() + ".fileprovider";
	}

	private Typeface getTextFont(int font){
		/*
		 * 0：无字体
		 * 1：原本字体
		 * 2：粗大字体
		 * 3：自定义字体
		 */
		switch (font){
			case 0:
				return null;
			case 1:
			default:
				return createTypeFace("fonts/Pixel.ttf");
			case 2:
				return createTypeFace("fonts/Pixel2.ttf");
			case 3:
				return createTypeFaceByFile(AppToolUtil.getDiyFontPath(this));
		}
	}
	private Typeface createTypeFace(String path){
		return Typeface.createFromAsset(getAssets(), path);
	}

	private Typeface createTypeFaceByFile(String path){
		return Typeface.createFromFile(path);
	}


    
}
